const rdp = require('./rdp/index')

module.exports = function (server, listen={}) {
	const io = require('socket.io')(server, { path: '/socket.io' })
	io.on('connection', function(client) {
		var rdpClient = null;
		client.on('infos', function (infos) {
			if (rdpClient) {
				rdpClient.close();
			}
			listen.connecting && listen.connecting(null, infos)
			rdpClient = rdp.createClient({ 
				userName: infos.username,
				password: infos.password,
				autoLogin: infos.username && infos.password,
				enablePerf : true,
				decompress: false,
				screen : infos.screen,
				updateLimit: infos.updateLimit || 100,
				locale : infos.locale,
				logLevel : process.argv[2] || 'INFO'
			}).on('connect', function () {
				client.emit('rdp-connect');
				listen.connect && listen.connect(client, infos)
			}).on('bitmap', function(bitmap) {
				client.emit('rdp-bitmap', bitmap)
			}).on('bitmaps', function(bitmaps) {
				client.emit('rdp-bitmaps', bitmaps)
			}).on('clipboard', function (content) {
				client.emit('rdp-clipboard', content);
				listen.clipboard && listen.clipboard(client, infos)
			}).on('close', function() {
				client.emit('rdp-close');
				listen.close && listen.close(client, infos)
			}).on('error', function(err) {
				client.emit('rdp-error', err);
				listen.error && listen.error(client, infos)
			}).connect(infos.ip, infos.port);
		}).on('connectid', function (infos) {
			if (rdpClient) {
				rdpClient.close();
			}
			const data = listen.getServer && listen.getServer(infos.id)
			infos.ip = data.ip || '127.0.0.1'
			infos.port = data.port || 3389
			infos.username = data.username
			infos.password = data.password
			listen.connecting && listen.connecting(null, infos)
			rdpClient = rdp.createClient({ 
				userName: infos.username,
				password: infos.password,
				enablePerf : false,
				decompress: false,
				screen : infos.screen,
				updateLimit: infos.updateLimit || 100,
				locale : infos.locale,
				logLevel : process.argv[2] || 'INFO'
			}).on('connect', function () {
				client.emit('rdp-connect');
				listen.connect && listen.connect(client, infos)
			}).on('bitmap', function(bitmap) {
				client.emit('rdp-bitmap', bitmap)
			}).on('bitmaps', function(bitmaps) {
				client.emit('rdp-bitmaps', bitmaps)
			}).on('clipboard', function (content) {
				client.emit('rdp-clipboard', content);
				listen.clipboard && listen.clipboard(client, infos)
			}).on('close', function() {
				client.emit('rdp-close');
				listen.close && listen.close(client, infos)
			}).on('error', function(err) {
				client.emit('rdp-error', err);
				listen.error && listen.error(client, infos)
			}).connect(infos.ip, infos.port);
		})
		.on('mouse', function (x, y, button, isPressed) {
			if (!rdpClient)  return;
			rdpClient.sendPointerEvent(x, y, button, isPressed);
		})
		.on('wheel', function (x, y, step, isNegative, isHorizontal) {
			if (!rdpClient) { return; }
			rdpClient.sendWheelEvent(x, y, step, isNegative, isHorizontal);
		})
		.on('scancode', function (code, isPressed, extended=false) {
			if (!rdpClient) return;
			if([0xE047, 0xE048, 0xE049, 0xE04B, 0xE04D, 0xE04F, 0xE050, 0xE051, 0xE052, 0xE053].includes(code)){
				rdpClient.sendKeyEventScancode(code, isPressed, true);
			}else{
				rdpClient.sendKeyEventScancode(code, isPressed, false);
			}
		})
		.on('unicode', function (code, isPressed, extended=false) {
			if (!rdpClient) return;
			rdpClient.sendKeyEventUnicode(code, isPressed, extended);
		})
		.on('clipboard', function (content) {
			if (!rdpClient) return;
			rdpClient.setClipboardData(content);
		})
		.on("logout", function () {
			if (!rdpClient) return;
			rdpClient.close();
		})
		.on('disconnect', function() {
			if(!rdpClient) return;
			rdpClient.close();
		})
	})
	return io
}